﻿using System;
using System.Collections.Generic;
using System.Linq;
using GC_FFW.Common.Helpers;
using GC_FFW.SoccerfameParser.Abstractions.Interfaces.Factories;
using GC_FFW.SoccerfameParser.Abstractions.Interfaces.Services;
using GC_FFW.SoccerfameParser.Enums;

namespace GC_FFW.SoccerfameParser.Services
{
	public class ImportService : IImportService
	{
		#region Variables

		private readonly ISoccerfameDataHelperFactory _dataHelperFactory;

		#endregion

		#region Constructors

		public ImportService(ISoccerfameDataHelperFactory dataHelperFactory)
		{
			if (dataHelperFactory == null)
				throw new ArgumentNullException(string.Format("dataHelperFactory"));
			_dataHelperFactory = dataHelperFactory;
		}

		#endregion

		#region IImportService Members

		public bool ImportNames(string filePath, ImportDataType type)
		{
			try
			{
				var service = _dataHelperFactory.CreateNamesService(filePath);
				if (service == null) return false;

				var data = service.Get() ?? new Dictionary<string, string>();
				if (data.Any())
				{
					INamesService dataService = null;

					switch (type)
					{
						case ImportDataType.SoccerfamePlayerNames:
							dataService = _dataHelperFactory.PlayerNamesService;
							break;

						case ImportDataType.SoccerfameTeamNames:
							dataService = _dataHelperFactory.TeamNamesService;
							break;
					}

					if (dataService != null)
					{
						dataService.Save(data);
					}
				}

				return true;
			}
			catch (Exception ex)
			{
				ApplicationLogger.Logger.LoggingExceptionMessage(ex);
				return false;
			}
		}

		#endregion
	}
}
